import asyncio
from bleak import BleakClient, BleakScanner
import struct
import pyautogui

# 替换为你的手机蓝牙鼠标应用程序的设备地址
device_address = "XX:XX:XX:XX:XX:XX"

# 鼠标服务和特征 UUID，可能需要根据使用的应用调整
mouse_service_uuid = "00001812-0000-1000-8000-00805f9b34fb"
mouse_char_uuid = "00002a32-0000-1000-8000-00805f9b34fb"

# 鼠标滑动的数据格式
def create_mouse_move_payload(x, y):
    # 假设使用标准的鼠标移动数据格式
    # 这里假设每轴用2字节表示，具体格式可能需要根据应用调整
    return struct.pack("bb", x, y)

async def run():
    # 扫描并连接到设备
    print(f"Connecting to device {device_address}...")
    async with BleakClient(device_address) as client:
        print(f"Connected to {device_address}")

        # 确保连接到正确的服务和特征
        if client.services.get_uuid(mouse_service_uuid):
            print("Mouse service found")
            mouse_char = client.services.get_characteristic(mouse_char_uuid)
            if mouse_char:
                print("Mouse characteristic found")

                # 模拟鼠标滑动
                for _ in range(10):  # 滑动10次
                    x_movement = 100  # 替换为需要的x轴移动量
                    y_movement = 50   # 替换为需要的y轴移动量
                    payload = create_mouse_move_payload(x_movement, y_movement)
                    await client.write_gatt_char(mouse_char, payload)
                    print(f"Sent movement: x={x_movement}, y={y_movement}")
                    await asyncio.sleep(1)  # 等待1秒
            else:
                print("Mouse characteristic not found")
        else:
            print("Mouse service not found")

# 运行异步任务
asyncio.run(run())